package com.atguigu.dga.thread;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadDemo2 {
    public static void main(String[] args) throws InterruptedException {
        /*
        线程池用来提交线程
        没有线程池
            new Thread().start;
            new Thread().start;
            new Thread().start;

        有线程池
            executorService.submit(new Thread()):运行一次submit是提交一次线程
         */

        ExecutorService executorService = Executors.newFixedThreadPool(10);

        long start = System.currentTimeMillis();

        for (int i = 0; i < 10; i++) {
            int finalI = i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    //耗时操作
                    try {
                        Thread.sleep(3000);
                    }catch (Exception e){
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName() + "  " + finalI + "表已经考核完毕！");
                }
            });
            //线程池关闭，不再向线程提交新的任务
            executorService.shutdown();

            long end = System.currentTimeMillis();

            //阻塞，等所有线程运行完毕，再打印下面这行
            executorService.awaitTermination(3, TimeUnit.MINUTES);
            System.out.println(Thread.currentThread().getName() + "耗时:" + (end - start));
        }
    }
}
